package com.castrianni.vcs;

import java.awt.Color;
import java.awt.Component;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.jgraph.JGraph;
import org.jgraph.graph.CellView;
import org.jgraph.graph.CellViewRenderer;
import org.jgraph.graph.DefaultCellViewFactory;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.VertexRenderer;
import org.jgraph.graph.VertexView;
import com.intellij.openapi.project.Project;
import info.clearthought.layout.TableLayout;

public class VcsRevisionGraphCellViewFactory extends DefaultCellViewFactory {
  public static final int CELL_MARGIN = 3;

  protected Color _revisionDeletedColor = new Color(218,230,218);
  protected Color _branchColor = new Color(184,230,230);
  protected Color _branchSelectedColor = new Color(51,128,128);
  protected Color _revisionColor = new Color(185,230,184);
  protected Color _revisionSelectedColor = new Color(53,128,51);
  protected Color _revisionDeletedSelectedColor = new Color(109,128,108);
  protected VcsRevisionVertexRenderer _revisionCellRenderer = null;
  protected VcsBranchVertexRenderer _branchCellRenderer = null;
  protected String _currentRevision = null;
  protected Project _project = null;

  public VcsRevisionGraphCellViewFactory(Project project)
  {
    _project = project;
    _revisionCellRenderer = createRevisionVertexRenderer();
    _branchCellRenderer = createBranchVertexRenderer();
  }

  protected VcsRevisionVertexRenderer createRevisionVertexRenderer()
  {
    return(new VcsRevisionVertexRenderer());
  }

  protected VcsBranchVertexRenderer createBranchVertexRenderer()
  {
    return(new VcsBranchVertexRenderer());
  }

  public void setCurrentRevision(String currentRevision)
  {
    _currentRevision = currentRevision;
  }

  protected VertexView createVertexView(Object cell)
  {
    DefaultGraphCell dgc = (DefaultGraphCell)cell;
    Object userObj = dgc.getUserObject();
    if(userObj instanceof VcsRevisionGraphRevision)
      return(new RevisionVertexView(dgc));
    else if(userObj instanceof VcsBranch)
      return(new BranchVertexView(dgc));
    else
      return(null);
  }

  protected class BranchVertexView extends VertexView {
    public BranchVertexView(Object cell)
    {
      super(cell);
    }

    public CellViewRenderer getRenderer()
    {
      return(_branchCellRenderer);
    }
  }

  protected class RevisionVertexView extends VertexView {
    public RevisionVertexView(Object cell)
    {
      super(cell);
    }

    public CellViewRenderer getRenderer()
    {
      return(_revisionCellRenderer);
    }
  }

  protected class VcsBranchVertexRenderer extends VertexRenderer {
    protected JPanel _panel = null;
    protected JLabel _name = null;

    public VcsBranchVertexRenderer()
    {
      double tableSizes[][] = {{TableLayout.FILL},{TableLayout.PREFERRED}};
      TableLayout tl = new TableLayout(tableSizes);
      tl.setHGap(5);
      tl.setVGap(5);
      _panel = new JPanel(tl);
      _panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black,1),BorderFactory.createEmptyBorder(CELL_MARGIN,CELL_MARGIN,CELL_MARGIN,CELL_MARGIN)));
      _name = new JLabel();
      _panel.add(_name,"0,0,c,f");
    }

    public Component getRendererComponent(JGraph graph, CellView view,boolean selected,boolean focus,boolean preview)
    {
      DefaultGraphCell cell = (DefaultGraphCell)view.getCell();
      VcsBranch branch = (VcsBranch)cell.getUserObject();
      _name.setText(branch.getName());
      if(selected) {
        _panel.setBackground(_branchSelectedColor);
        _name.setForeground(Color.white);
      } else {
        _panel.setBackground(_branchColor);
        _name.setForeground(Color.black);
      }
      return(_panel);
    }
  }

  protected class VcsRevisionVertexRenderer extends VertexRenderer {
    protected JPanel _panel = null;
    protected JLabel _author = null;
    protected JLabel _revision = null;

    public VcsRevisionVertexRenderer()
    {
      double tableSizes[][] = {{TableLayout.FILL,TableLayout.FILL},{TableLayout.PREFERRED}};
      TableLayout tl = new TableLayout(tableSizes);
      tl.setHGap(5);
      _panel = new JPanel(tl);
      _revision = new JLabel();
      _panel.add(_revision,"0,0,l,f");
      _author = new JLabel();
      _panel.add(_author,"1,0,r,f");
    }

    public Component getRendererComponent(JGraph graph,CellView view,boolean selected,boolean focus,boolean preview)
    {
      DefaultGraphCell cell = (DefaultGraphCell)view.getCell();
      VcsRevisionGraphRevision rev = (VcsRevisionGraphRevision)cell.getUserObject();
      _revision.setText(rev.getRevisionNumber().asString());
      if(rev.getRevisionNumber().asString().equals(_currentRevision))
        _panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black,4),BorderFactory.createEmptyBorder(CELL_MARGIN,CELL_MARGIN,CELL_MARGIN,CELL_MARGIN)));
      else
        _panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black,1),BorderFactory.createEmptyBorder(CELL_MARGIN,CELL_MARGIN,CELL_MARGIN,CELL_MARGIN)));
      _author.setText(rev.getAuthor());
      if(selected) {
        _panel.setBackground(_revisionSelectedColor);
        _revision.setForeground(Color.white);
        _author.setForeground(Color.white);
      } else {
        _panel.setBackground(_revisionColor);
        _revision.setForeground(Color.black);
        _author.setForeground(Color.black);
      }
      return(_panel);
    }
  }
}
